package com.taskcollection.service;

import com.taskcollection.bean.ProjectBean;
import com.taskcollection.mapper.ProjectMapper;
import com.taskcollection.util.DBConnection;
import com.taskcollection.util.DBUtil;
import com.taskcollection.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by haocongshun on 2016/7/26.
 */
public class ProjectService {
    private ProjectMapper prom;

    public ProjectMapper getProm() {
        return prom;
    }

    public void setProm(ProjectMapper prom) {
        this.prom = prom;
    }

    public List<ProjectBean> getAll(String teacher_id){
        Map<String ,Object> para = new HashMap<String ,Object>();
        para.put("teacher_id",teacher_id);
        List<ProjectBean> projectList = prom.getAll(para);
        return projectList;
    }
    public boolean add(ProjectBean pb){
        String sql = "insert into projectinfo(Project_id,Teacher_id,ProjectTeam_id,Student_id,project_name,project_isSet,project_introduction,ProjectClassify_id) values(?,?,?,?,?,?,?,?)";
        Connection conn = DBConnection.getinstance().getConnection();
        PreparedStatement ps= null;
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,ServiceUtil.getPK("Project_id","projectinfo"));
            ps.setString(2,pb.getTeacher_id());
            ps.setString(3,pb.getProjectTeam_id());
            ps.setString(4,pb.getStudent_id());
            ps.setString(5,pb.getProject_name());
            ps.setString(6,pb.getProject_isSet());
            ps.setString(7,pb.getProject_introduction());
            ps.setString(8,pb.getProjectClassify_id());
            ps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closePreparedStatement(ps);
            DBUtil.closeConnection(conn);
        }
        return false;
    }
    public boolean delete(String id){
        String sql = "delete * from projectinfo where Project_id=?";
        Connection conn = DBConnection.getinstance().getConnection();
        PreparedStatement ps = null;
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,id);
            ps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closePreparedStatement(ps);
            DBUtil.closeConnection(conn);
        }
        return false;
    }
    public boolean change(ProjectBean pb,boolean setTeam){
        String sql ="update projectinfo set teacher_id=?,projectTeam_id=?,student_id=?,project_name=?,project_isSet=?,project_introduction=?,projectClassify_id=? where project_id=?";
        String sql1="insert into projectteam(ProjectTeam_id,projectTeam_name,projectTeam_count) values(?,?,?)";
        Connection conn = DBConnection.getinstance().getConnection();
        PreparedStatement ps = null;
        PreparedStatement ps1 = null;
        try{
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(sql);
            ps.setString(1,pb.getTeacher_id());
            ps.setString(2,pb.getProjectTeam_id());
            ps.setString(3,pb.getStudent_id());
            ps.setString(4,pb.getProject_name());
            ps.setString(5,pb.getProject_isSet());
            ps.setString(6,pb.getProject_introduction());
            ps.setString(7,pb.getProjectClassify_id());
            ps.setString(8,pb.getProject_id());
            if(setTeam){
                ps.setString(2,ServiceUtil.getPK("projectTeam_id","projectteam"));
                ps1=conn.prepareStatement(sql1);
                ps1.setString(1,ServiceUtil.getPK("projectTeam_id","projectteam"));
                ps1.setString(2,pb.getProject_name());
                ps1.setString(3,"0");
                ps1.execute();
            }
            ps.execute();
            conn.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closePreparedStatement(ps);;
            DBUtil.closePreparedStatement(ps1);
            DBUtil.closeConnection(conn);
        }
        return false;
    }
    public ProjectBean getById(String id){
        String sql = "select * from projectinfo where project_id=?";
        Connection conn = DBConnection.getinstance().getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        ProjectBean pb = new ProjectBean();
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                pb.setProject_id(rs.getString("project_id"));
                pb.setTeacher_id(rs.getString("teacher_id"));
                pb.setProjectTeam_id(rs.getString("projectTeam_id"));
                pb.setStudent_id(rs.getString("student_id"));
                pb.setProject_name(rs.getString("project_name"));
                pb.setProject_isSet(rs.getString("project_isSet"));
                pb.setProject_introduction(rs.getString("project_introduction"));
                pb.setProjectClassify_id(rs.getString("ProjectClassify_id"));
                TeacherService ts = new TeacherService();
                pb.setTeacher(ts.getById(pb.getTeacher_id()));
                StudentService ss = new StudentService();
                pb.setStudent(ss.getById(pb.getStudent_id()));
                ProjectTeamService pts = new ProjectTeamService();
                pb.setProjectTeam(pts.getById(pb.getProjectTeam_id()));
                ProjectClassifyService pcs = new ProjectClassifyService();
                pb.setProjectClassify(pcs.getById(pb.getProjectClassify_id()));
                return pb;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            DBUtil.closeConnection(conn);
        }
        return null;
    }
}
